package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PictureAdapter;
import flc.ast.databinding.ActivityPicConvertPdfBinding;
import flc.ast.dialog.ConvertTipDialog;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class PicConvertPdfActivity extends BaseAc<ActivityPicConvertPdfBinding> {

    private PictureAdapter pictureAdapter;
    private List<String> listSel = new ArrayList<>();


    @Override
    protected int onCreate() {
        return R.layout.activity_pic_convert_pdf;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPicConvertPdfBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvImport.setOnClickListener(this);

        mDataBinding.rvPictureList.setLayoutManager(new LinearLayoutManager(mContext));
        pictureAdapter = new PictureAdapter();
        mDataBinding.rvPictureList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvImport://导入
                if (listSel.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_picture));
                    return;
                }
                showConvertTipDialog();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (pictureAdapter.getItem(position).isChecked()) {
            pictureAdapter.getItem(position).setChecked(false);
            listSel.remove(pictureAdapter.getItem(position).getPath());
        } else {
            pictureAdapter.getItem(position).setChecked(true);
            listSel.add(pictureAdapter.getItem(position).getPath());
        }
        pictureAdapter.notifyItemChanged(position);
        mDataBinding.tvImport.setText(getString(R.string.import_num, listSel.size()));
    }

    @Override
    protected void initData() {
        mDataBinding.tvImport.setText(getString(R.string.import_num, 0));
        getData();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    pictureAdapter.setList(selectMediaEntities);
                    mDataBinding.rvPictureList.setVisibility(View.VISIBLE);
                    mDataBinding.tvPictureNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPictureList.setVisibility(View.GONE);
                    mDataBinding.tvPictureNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void showConvertTipDialog() {
        ConvertTipDialog dialog = new ConvertTipDialog(mContext);
        dialog.type = 1;
        dialog.listSel = listSel;

        dialog.show();
    }

}